package com.cenntro.oss.interceptor;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.boot.jackson.JsonComponent;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

@JsonComponent
public class JsonDateDeserializer   extends JsonDeserializer<Date> {
 private String[] patterns = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"};

//	@Override
	public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
		String dateAsString = jsonParser.getText();
		Date parseDate = null;
		try {
			parseDate = DateUtils.parseDate(dateAsString, patterns);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return parseDate;
	}
	
}